﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace moveErrorFile.classes
{
    class clsFileHelper
    {
        public clsFileHelper()
        {

        }
        
        public static List<FileInfo> GetTodayFileInDirectory(DirectoryInfo directoryInfo)
        {
            FileInfo[] files = directoryInfo.GetFiles();
            List<FileInfo> TodayFiles = new List<FileInfo>();

            foreach (FileInfo file in files)
            {
                DateTime today = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
                DateTime fileCreateDate = new DateTime(file.CreationTime.Year, file.CreationTime.Month, file.CreationTime.Day);

                if (today.Equals(fileCreateDate))
                {
                    TodayFiles.Add(file);
                }
            }

            return TodayFiles;
        }

        public static void MoveErrorFile(out string sErrorMsg, string sOriginPath, string sDestPath)
        {
            sErrorMsg = null;

            try
            {
                if (!File.Exists(sOriginPath))
                {
                    sErrorMsg = "File : " + sOriginPath + " does not exist!!!";
                    return;
                }

                // Move the file.
                File.Move(sOriginPath, sDestPath);

                // See if the original exists now. 
                if (File.Exists(sDestPath))
                {
                    sErrorMsg = "The original file still exists, which is unexpected.";
                }
                else
                {
                    File.Move(sOriginPath, sDestPath);
                }

            }
            catch (Exception ex)
            {
                sErrorMsg = ex.Message;
            }
        }


    }
}
